// src/main/java/org/example/backend/repository/StudentRepository.java
package org.example.backend.repository;

import org.example.backend.entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.Optional;

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
    Optional<Student> findByUserId(Long userId);
    Optional<Student> findByStudentNumber(String studentNumber);

    @Query("SELECT s FROM Student s JOIN Internship i ON s.id = i.studentId WHERE i.teacherId = :teacherId")
    Page<Student> findByTeacherId(@Param("teacherId") Long teacherId, Pageable pageable);

    @Query("SELECT CASE WHEN COUNT(s) > 0 THEN true ELSE false END FROM Student s JOIN Internship i ON s.id = i.studentId WHERE i.teacherId = :teacherId AND s.id = :studentId")
    boolean existsByTeacherIdAndId(@Param("teacherId") Long teacherId, @Param("studentId") Long studentId);
}